<?php
class Pager {
	/**
	 *int 总页数
	 protected $pageTotal;
	 /**
	 *int 上一页
	 * */
	protected $previous;
	/**
	 *int 下一页
	 * */
	protected $next;
	/**
	 *int 中间页起始序号
	 * */   protected $startPage;
	/**
	 *int 中间页终止序号
	 * */    protected $endPage;
	/**
	 *int 记录总数
	 * */    protected $recorbTotal;
	/**
	 *int 每页显示记录数
	 * */     protected $pageSize;
	/**
	 *int 当前显示页
	 * */    protected $currentPage;
	/**
	 *string 基url地址
	 * */     protected $baseUri;

	/**
	 * @return string 获取基url地址
	 */     public function getBaseUri() {
	return $this->baseUri;
	 }

	 /**
	  * @return int 获取当前显示页
	  */    public function getCurrentPage() {
	 return $this->currentPage;
	  }

	  /**
	   * @return int 获取每页显示记录数
	   */
	  public function getPageSize() {
	  	return $this->pageSize;
	  }

	  /**
	   * @return int 获取记录总数
	   */
	  public function getRecorbTotal() {
	   return $this->recorbTotal;
	  }

	  /**
	   * @param string $baseUri 设置基url地址
	   */
	  public function setBaseUri($baseUri) {
	  	$this->baseUri = $baseUri;
	  }

	  /**
	   * @param int $currentPage 设置当前显示页
	   */    public function setCurrentPage($currentPage) {
	  $this->currentPage=$currentPage;
	   }

	   /**
	    * @param int $pageSize 设置每页显示记录数
	    */
	   public function setPageSize($pageSize) {
	   	$this->pageSize = $pageSize;
	   }

	   /**
	    * @param int $recorbTotal 设置获取记录总数
	    */
	   public function setRecorbTotal($recorbTotal) {
	   	$this->recorbTotal = $recorbTotal;
	   }

	   /**
	    *构造函数
	    * */
	   public function __construct()
	   {
	   	$this->pageTotal = 0;
	   	$this->previous = 0;
	   	$this->next = 0;
	   	$this->startPage = 0;
	   	$this->endPage = 0;

	   	$this->pageSize = 20;
	   	$this->currentPage = 0;
	   }

	   /**
	    *分页算法
	    * */
	   private function arithmetic() {
	   	if ($this->currentPage < 1)
	   	$this->currentPage = 1;

	   	$this->pageTotal = floor ( $this->recorbTotal / $this->pageSize ) + ($this->recorbTotal % $this->pageSize > 0 ? 1 : 0);

	   	if ($this->currentPage > 1 && $this->currentPage > $this->pageTotal)
	   	header ( 'location:' . $this->baseUri . 'page=' . $this->pageTotal );

	   	$this->next = $this->currentPage + 1;
	   	$this->previous = $this->currentPage - 1;

	   	$this->startPage = ($this->currentPage + 5) > $this->pageTotal ? $this->pageTotal - 10 : $this->currentPage - 5;
	   	$this->endPage = $this->currentPage < 5 ? 11 : $this->currentPage + 5;

	   	if ($this->startPage < 1)
	   	$this->startPage = 1;

	   	if ($this->pageTotal < $this->endPage)
	   	$this->endPage = $this->pageTotal;
	   }

	   /**
	    *分页样式
	    * */
	   protected function pageStyle() {
	   	$result = "共" . $this->pageTotal . "页      ";

	   	 
	   	for($i = $this->startPage; $i <= $this->endPage; $i ++) {
	   		if ($this->currentPage == $i){
	   			$result .= "  <font color=\"#ff0000\">$i</font>";
	   		}
	   		else{
	   			$result .= "  <a href=\"" . $this->baseUri . "page=$i\">$i</a>";
	   		}
	   	}

	   
	   	return $result;
	   }

	   /**
	    *执行分页
	    * */
	   public function execute() {
	   	if ($this->baseUri != "" && $this->recorbTotal == 0)
	   	return "";
	   	$this->arithmetic();
	   	return $this->pageStyle ();
	   }
}
?>
